package com.dmooo.yxmmerchant.util;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.common.Config;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.io.File;

public class SelectPicUtil {
    public static  Uri tempUri;
    public static void selectPic(Activity context, int num, int requestCode) {
        if (requestCode == Config.SELECT_PHOTO) {
            Matisse.from(context)
                    .choose(MimeType.of(MimeType.JPEG, MimeType.PNG))
                    .countable(false)
                    .theme(R.style.MyPicSelectTheme)
                    .maxSelectable(num)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .imageEngine(new GlideEngine())
                    .forResult(requestCode);
        } else {
            Intent openCameraIntent = new Intent(
                    MediaStore.ACTION_IMAGE_CAPTURE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            tempUri = FileProvider.getUriForFile(context, "com.dmooo.yzxmerchant",
                    new File(Environment.getExternalStorageDirectory(), "icon.jpg"));
            openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
            context.startActivityForResult(openCameraIntent, Config.TAKE_PHOTO);
        }
    }
}
